package com.xkdx.caipiao.hall;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.listener.OnBallClickListener;
import com.xkdx.caipiao.listener.ShakeListener;
import com.xkdx.caipiao.listener.ShakeListener.OnShakeListener;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.quert.IssueQueryAction;
import com.xkdx.caipiao.presistence.quert.IssueQueryInfo;
import com.xkdx.caipiao.presistence.quert.IssueQueryModule;
import com.xkdx.caipiao.presistence.quert.IssueQueryPresistence;
import com.xkdx.caipiao.pubclass.SelectCode;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.CaipiaoUtil;
import com.xkdx.caipiao.util.SharePrefenceUtil;
import com.xkdx.caipiao.wiget.BlueBallButton;
import com.xkdx.caipiao.wiget.WhiteBallButton;

public class LottoActivity extends CaiPiaoActivity implements OnBallClickListener {

	List<String> redNumber = new ArrayList<String>();
	List<String> blueNumber = new ArrayList<String>();
	public static ArrayList<String> singeRedSet = new ArrayList<String>();
	public static ArrayList<String> singeBlueSet = new ArrayList<String>();

	ShakeListener mShakeListener;

	Vibrator mVibrator01;
	View lottoView;
	TextView mutiply_tv;
	IssueQueryAction action;
	IssueQueryModule module;
	IssueQueryPresistence presistence;

	private final static int RED = 1;
	private final static int BLUE = 2;
	public WhiteBallButton tv01, tv02, tv03, tv04, tv05, tv06, tv07, tv08,
			tv09, tv10, tv11, tv12, tv13, tv14, tv15, tv16, tv17, tv18, tv19,
			tv20, tv21, tv22, tv23, tv24, tv25, tv26, tv27, tv28, tv29, tv30,
			tv31, tv32, tv33,tv34,tv35;

	public BlueBallButton bluetv01, bluetv02, bluetv03, bluetv04, bluetv05,
			bluetv06, bluetv07, bluetv08, bluetv09, bluetv10, bluetv11,
			bluetv12;
	int num = 0;
	int money = 0;
	String singleOrDouble = "单式";
	private PopupWindow popupWindow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lotto_main_caipiao);
		init();
		startThread();
		
	}

	@Override
	protected void onResume() {

		super.onResume();
		singeRedSet.clear();
		singeBlueSet.clear();

	}

	public void onRightClick(View view) {
		Intent intent = new Intent(this, LottoRuleActivity.class);
		startActivity(intent);
	}

	// 跳转页面
	public void onBetClick(View view) {

		IsIntent(); // 判断是否需要自动选好。如果没有选择任何号码就投注则自动选7个号码。如果人为手动选好，小于5和2，则提示不够一注
	}

	private void IsIntent() {
		if (singeRedSet.size() == 0 && singeBlueSet.size() == 0) {
			produceNumbers();
			findNumbeText(lottoView, singeRedSet, singeBlueSet);
			setMutiplyList();

		} else {

			if (singeRedSet.size() < 5 || singeBlueSet.size() < 2) {

				Toast.makeText(getApplicationContext(), "请选择至少一注", 0).show();

			} else {

				getCaiPiaoObject(singeRedSet, singeBlueSet);
				Intent intent = new Intent(this, BetLottoActivity.class);
				startActivity(intent);
				finish();
			}

		}
	}

	private void init() {
		TextView tv_right = (TextView) findViewById(R.id.top_right_tv);
		tv_right.setText(getResources().getString(R.string.game_rule));
		mutiply_tv = (TextView) findViewById(R.id.mutiply_tv);
		lottoView = (View) findViewById(R.id.lotto);

		tv01 = (WhiteBallButton) findViewById(R.id.ball_01);
		tv02 = (WhiteBallButton) findViewById(R.id.ball_02);
		tv03 = (WhiteBallButton) findViewById(R.id.ball_03);
		tv04 = (WhiteBallButton) findViewById(R.id.ball_04);
		tv05 = (WhiteBallButton) findViewById(R.id.ball_05);
		tv06 = (WhiteBallButton) findViewById(R.id.ball_06);
		tv07 = (WhiteBallButton) findViewById(R.id.ball_07);
		tv08 = (WhiteBallButton) findViewById(R.id.ball_08);
		tv09 = (WhiteBallButton) findViewById(R.id.ball_09);
		tv10 = (WhiteBallButton) findViewById(R.id.ball_10);
		tv11 = (WhiteBallButton) findViewById(R.id.ball_11);
		tv12 = (WhiteBallButton) findViewById(R.id.ball_12);
		tv13 = (WhiteBallButton) findViewById(R.id.ball_13);
		tv14 = (WhiteBallButton) findViewById(R.id.ball_14);
		tv15 = (WhiteBallButton) findViewById(R.id.ball_15);
		tv16 = (WhiteBallButton) findViewById(R.id.ball_16);
		tv17 = (WhiteBallButton) findViewById(R.id.ball_17);
		tv18 = (WhiteBallButton) findViewById(R.id.ball_18);
		tv19 = (WhiteBallButton) findViewById(R.id.ball_19);
		tv20 = (WhiteBallButton) findViewById(R.id.ball_20);
		tv21 = (WhiteBallButton) findViewById(R.id.ball_21);
		tv22 = (WhiteBallButton) findViewById(R.id.ball_22);
		tv23 = (WhiteBallButton) findViewById(R.id.ball_23);
		tv24 = (WhiteBallButton) findViewById(R.id.ball_24);
		tv25 = (WhiteBallButton) findViewById(R.id.ball_25);
		tv26 = (WhiteBallButton) findViewById(R.id.ball_26);
		tv27 = (WhiteBallButton) findViewById(R.id.ball_27);
		tv28 = (WhiteBallButton) findViewById(R.id.ball_28);
		tv29 = (WhiteBallButton) findViewById(R.id.ball_29);
		tv30 = (WhiteBallButton) findViewById(R.id.ball_30);
		tv31 = (WhiteBallButton) findViewById(R.id.ball_31);
		tv32 = (WhiteBallButton) findViewById(R.id.ball_32);
		tv33 = (WhiteBallButton) findViewById(R.id.ball_33);
		tv34 = (WhiteBallButton) findViewById(R.id.ball_34);
		tv35 = (WhiteBallButton) findViewById(R.id.ball_35);

		tv01.setBallClickListener(this);
		tv02.setBallClickListener(this);
		tv03.setBallClickListener(this);
		tv04.setBallClickListener(this);
		tv05.setBallClickListener(this);
		tv06.setBallClickListener(this);
		tv07.setBallClickListener(this);
		tv08.setBallClickListener(this);
		tv09.setBallClickListener(this);
		tv10.setBallClickListener(this);
		tv11.setBallClickListener(this);
		tv12.setBallClickListener(this);
		tv13.setBallClickListener(this);
		tv14.setBallClickListener(this);
		tv15.setBallClickListener(this);
		tv16.setBallClickListener(this);
		tv17.setBallClickListener(this);
		tv18.setBallClickListener(this);
		tv19.setBallClickListener(this);
		tv20.setBallClickListener(this);
		tv21.setBallClickListener(this);
		tv22.setBallClickListener(this);
		tv23.setBallClickListener(this);
		tv24.setBallClickListener(this);
		tv25.setBallClickListener(this);
		tv26.setBallClickListener(this);
		tv27.setBallClickListener(this);
		tv28.setBallClickListener(this);
		tv29.setBallClickListener(this);
		tv30.setBallClickListener(this);
		tv31.setBallClickListener(this);
		tv32.setBallClickListener(this);
		tv33.setBallClickListener(this);
		tv34.setBallClickListener(this);
		tv35.setBallClickListener(this);

		tv01.setMark("red");
		tv02.setMark("red");
		tv03.setMark("red");
		tv04.setMark("red");
		tv05.setMark("red");
		tv06.setMark("red");
		tv07.setMark("red");
		tv08.setMark("red");
		tv09.setMark("red");
		tv10.setMark("red");
		tv11.setMark("red");
		tv12.setMark("red");
		tv13.setMark("red");
		tv14.setMark("red");
		tv15.setMark("red");
		tv16.setMark("red");
		tv17.setMark("red");
		tv18.setMark("red");
		tv19.setMark("red");
		tv20.setMark("red");
		tv21.setMark("red");
		tv22.setMark("red");
		tv23.setMark("red");
		tv24.setMark("red");
		tv25.setMark("red");
		tv26.setMark("red");
		tv27.setMark("red");
		tv28.setMark("red");
		tv29.setMark("red");
		tv30.setMark("red");
		tv31.setMark("red");
		tv32.setMark("red");
		tv33.setMark("red");
		tv34.setMark("red");
		tv35.setMark("red");

		bluetv01 = (BlueBallButton) findViewById(R.id.blue_ball01);
		bluetv02 = (BlueBallButton) findViewById(R.id.blue_ball02);
		bluetv03 = (BlueBallButton) findViewById(R.id.blue_ball03);
		bluetv04 = (BlueBallButton) findViewById(R.id.blue_ball04);
		bluetv05 = (BlueBallButton) findViewById(R.id.blue_ball05);
		bluetv06 = (BlueBallButton) findViewById(R.id.blue_ball06);
		bluetv07 = (BlueBallButton) findViewById(R.id.blue_ball07);
		bluetv08 = (BlueBallButton) findViewById(R.id.blue_ball08);
		bluetv09 = (BlueBallButton) findViewById(R.id.blue_ball09);
		bluetv10 = (BlueBallButton) findViewById(R.id.blue_ball10);
		bluetv11 = (BlueBallButton) findViewById(R.id.blue_ball11);
		bluetv12 = (BlueBallButton) findViewById(R.id.blue_ball12);

		bluetv01.setBallClickListener(this);
		bluetv02.setBallClickListener(this);
		bluetv03.setBallClickListener(this);
		bluetv04.setBallClickListener(this);
		bluetv05.setBallClickListener(this);
		bluetv06.setBallClickListener(this);
		bluetv07.setBallClickListener(this);
		bluetv08.setBallClickListener(this);
		bluetv09.setBallClickListener(this);
		bluetv10.setBallClickListener(this);
		bluetv11.setBallClickListener(this);
		bluetv12.setBallClickListener(this);

		bluetv01.setMark("blue");
		bluetv02.setMark("blue");
		bluetv03.setMark("blue");
		bluetv04.setMark("blue");
		bluetv05.setMark("blue");
		bluetv06.setMark("blue");
		bluetv07.setMark("blue");
		bluetv08.setMark("blue");
		bluetv09.setMark("blue");
		bluetv10.setMark("blue");
		bluetv11.setMark("blue");
		bluetv12.setMark("blue");

		mShakeListener = new ShakeListener(this);
		mShakeListener.setOnShakeListener(new shakeLitener());

	}

	public void onBackClick(View view) {

		finish();
	}

	public void setMutiplyList() {
		if (singeRedSet.size() < 5 || singeBlueSet.size() < 2) {
			mutiply_tv.setText("");

		} else {
			setText();
		}
	}

	private void setText() {
		setReds();
		setBlus();
		num = redNumber.size() * blueNumber.size();
		money = 2 * num;

		singleOrDouble = num > 1 ? "复式" : "单式";

		String text = "共" + num + "注" + money + "元";
		mutiply_tv.setText(text);
	}

	private void setReds() {
		List<String> list = new ArrayList<String>();

		String[] a = new String[singeRedSet.size()];
		Iterator<String> iterator = singeRedSet.iterator();

		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
		for (int j = 0; j < list.size(); j++) {
			a[j] = list.get(j);
		}
		redNumber = CaipiaoUtil.ChooseSix(a, 5);
	}

	private void setBlus() {
		List<String> list = new ArrayList<String>();

		String[] a = new String[singeBlueSet.size()];
		Iterator<String> iterator = singeBlueSet.iterator();

		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
		for (int j = 0; j < list.size(); j++) {
			a[j] = list.get(j);
		}
		blueNumber = CaipiaoUtil.ChooseSix(a, 2);
	}

	private void findNumbeText(View view, ArrayList<String> redSet,
			ArrayList<String> blueSet) {
		setState(RED, redSet);
		setState(BLUE, blueSet);
	}

	private void setState(int type, ArrayList<String> hashSet) {
		if (type == 1) {
			tv01.setLottoState(hashSet);
			tv02.setLottoState(hashSet);
			tv03.setLottoState(hashSet);
			tv04.setLottoState(hashSet);
			tv05.setLottoState(hashSet);
			tv06.setLottoState(hashSet);
			tv07.setLottoState(hashSet);
			tv08.setLottoState(hashSet);
			tv09.setLottoState(hashSet);
			tv10.setLottoState(hashSet);
			tv11.setLottoState(hashSet);
			tv12.setLottoState(hashSet);
			tv13.setLottoState(hashSet);
			tv14.setLottoState(hashSet);
			tv15.setLottoState(hashSet);
			tv16.setLottoState(hashSet);
			tv17.setLottoState(hashSet);
			tv18.setLottoState(hashSet);
			tv19.setLottoState(hashSet);
			tv20.setLottoState(hashSet);
			tv21.setLottoState(hashSet);
			tv22.setLottoState(hashSet);
			tv23.setLottoState(hashSet);
			tv24.setLottoState(hashSet);
			tv25.setLottoState(hashSet);
			tv26.setLottoState(hashSet);
			tv27.setLottoState(hashSet);
			tv28.setLottoState(hashSet);
			tv29.setLottoState(hashSet);
			tv30.setLottoState(hashSet);
			tv31.setLottoState(hashSet);
			tv32.setLottoState(hashSet);
			tv33.setLottoState(hashSet);
			tv34.setLottoState(hashSet);
			tv35.setLottoState(hashSet);

		} else {

			bluetv01.setState(hashSet);
			bluetv02.setState(hashSet);
			bluetv03.setState(hashSet);
			bluetv04.setState(hashSet);
			bluetv05.setState(hashSet);
			bluetv06.setState(hashSet);
			bluetv07.setState(hashSet);
			bluetv08.setState(hashSet);
			bluetv09.setState(hashSet);
			bluetv10.setState(hashSet);
			bluetv11.setState(hashSet);
			bluetv12.setState(hashSet);

		}
	}

	private class shakeLitener implements OnShakeListener {
		@Override
		public void onShake() {

			produceNumbers();
			findNumbeText(lottoView, singeRedSet, singeBlueSet);
			setMutiplyList();

			mVibrator01 = (Vibrator) getApplication().getSystemService(
					Service.VIBRATOR_SERVICE);
			mVibrator01.vibrate(new long[] { 100, 10, 100, 100 }, -1);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
	}

	private void produceNumbers() {

		singeRedSet.clear();
		singeBlueSet.clear();

		mutiply_tv.setText(getResources().getString(R.string.bet_tip));

		Random random = new Random();

		String blue1 = "";
		int randomBlue = random.nextInt(12) + 1;
		blue1 = randomBlue < 10 ? "0" + randomBlue : randomBlue + "";
		int next = random.nextInt(12) + 1;

		while (randomBlue == next) {
			next = random.nextInt(12) + 1;
		}
		String blue2 = next < 10 ? "0" + next : next + "";
		singeBlueSet.add(blue2 + "");
		singeBlueSet.add(blue1 + "");

		while (singeRedSet.size() < 5) {

			int randomInt = random.nextInt(35) + 1;
			String newBall = randomInt < 10 ? "0" + randomInt : randomInt + "";
			if (!(singeRedSet.contains(newBall))) {
				String red = newBall;
				singeRedSet.add(red);
			}
		}

	}

	public void clearNumber(View view) {

		singeRedSet.clear();
		singeBlueSet.clear();
		setState(RED, singeRedSet);
		setState(BLUE, singeBlueSet);

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (result.containsKey("0")) {
			if (module.isSuccess) {
				SharePrefenceUtil su = new SharePrefenceUtil(this,
						SharePrefenceUtil.ISSUEQUERYINFO);
				su.saveObject(SharePrefenceUtil.ISSUEQUERYINFO,
						IssueQueryInfo.class);
				Toast.makeText(this, "获取期次成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "获取期次失败", Toast.LENGTH_SHORT).show();
			}
		} else {

		}

	}

	@Override
	public void startThread() {
//		action = new IssueQueryAction("dlt");
//		module = new IssueQueryModule();
//		presistence = new IssueQueryPresistence();
//		presistence.setActitons(action);
//		presistence.setModule(module);
//		presistence.execute();
	}

	private void getCaiPiaoObject(ArrayList<String> reds,
			ArrayList<String> blues) {
		StringBuffer sb = new StringBuffer();

		Collections.sort(reds);
		Collections.sort(blues);

		SelectCode code = new SelectCode();

		for (String red : reds) {

			sb.append(red).append(" ");
		}

		code.setRedCode(sb.toString());

		sb.setLength(0);
		for (String blue : blues) {

			sb.append(blue).append(" ");
		}
		code.setBlueCode(sb.toString());
		code.setMoneyPerBet(money + "");
		code.setBettingNum(num + "");
		code.setSingleOrDouble(singleOrDouble);
		// code.setCode(sb.toString());
		code.setCaizhong(getResources().getString(R.string.lotto));
		code.setBetMode(singleOrDouble + " " + mutiply_tv.getText().toString());
		IConstants.list_selectcode.add(code);

	}

	@Override
	public void RemoveBall(String type, String text) {
		if (type.equals("red")) {
			singeRedSet.remove(text);
		} else {
			singeBlueSet.remove(text);
		}
	}

	@Override
	public void AddBall(String type, String text) {
		if (type.equals("red")) {
			singeRedSet.add(text);
		} else {
			singeBlueSet.add(text);
		}
	}

	@Override
	public void ShowBallPopoWidow(View v,String text) {
		View view = getLayoutInflater().inflate(R.layout.popwindow_caipiao, null);
		
		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		TextView tv = (TextView) view.findViewById(R.id.text);
		tv.setText(text);
		RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.pop_rl);
		if(v instanceof WhiteBallButton){
			rl.setBackgroundResource(R.drawable.ball_red_clicked_caipiao);
		}else{
			rl.setBackgroundResource(R.drawable.ball_blue_clicked_caipiao);
		}
		
		int[] location = new int[2];
		v.getLocationOnScreen(location);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		int left = v.getLeft();
		int right =v.getRight();
		int top = v.getTop();
		int bottom = v.getBottom();
		
		int vWidth = (int) ((right - left) * 0.5);
		popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] - vWidth
				+ 4, location[1] - (int) (1.6 * (bottom - top))-10);
	}

	@Override
	public void DismissBallPopowindow() {
		if(popupWindow!=null){
			popupWindow.dismiss();
		}
		
	}


}